﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GIC.DAO.Model;
using GIC.BO.Access;

namespace GIC.UI
{
    public partial class SalvarChassi : Form
    {
        private Configuracoes form2;

        public SalvarChassi()
        {
            InitializeComponent();
            this.TopMost = true;
        }

        public SalvarChassi(Configuracoes form2)
        {
            InitializeComponent();
            this.form2 = form2;
            this.TopMost = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.txtChassis.Text == "")
            {
                MessageBox.Show("Não existem números de chassis para serem salvos!",
                        "Salvar Chassis",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation,
                        MessageBoxDefaultButton.Button1);
            }
            else
            {
                String[] chassis = this.txtChassis.Text.Split('\n');

                if (chassis != null && chassis.Length > 0)
                {
                    salvarChassis(chassis);
                }

                MessageBox.Show("Informações gravadas com sucesso!",
                                        "Salvar Chassis",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.None,
                                        MessageBoxDefaultButton.Button1);

                this.form2.Enabled = true;
                this.form2.Show();
                this.Close();
            }
        }

        private void salvarChassis(string[] chassis)
        {
            GICBo bo = new GICBo();

           foreach(String chassi in chassis)
           {
               if (chassi != "" && chassi != "\r")
               {
                   Chassi chassiObj = new Chassi();
                   chassiObj.bodyNumber = chassi.Trim().TrimEnd().TrimStart();
                   chassiObj.impresso = 0;
                   chassiObj.reportDate = DateTime.Now;

                   bo.salvar(chassiObj);
               }
           }
        }

        private void Form3_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.form2.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.form2.Enabled = true;
            this.Close();
        }
    }
}
